/*
 * @lc app=leetcode.cn id=219 lang=cpp
 *
 * [219] 存在重复元素 II
 */
#include <cmath>
#include <unordered_map>
#include <vector>

using std::unordered_map;
using std::vector;
// @lc code=start
class Solution
{
  public:
    bool containsNearbyDuplicate(vector<int> &nums, int k)
    {
        unordered_map<int, int> my_map;
        int n = nums.size();

        for (int i = 0; i < n; ++i) {
            if (my_map.find(nums[i]) != my_map.end()) {
                if (abs(my_map[nums[i]] - i) <= k) {
                    return true;
                } else {
                    my_map[nums[i]] = i;
                }
            } else {
                my_map[nums[i]] = i;
            }
        }
        return false;
    }
};
// @lc code=end

int main() { return 0; }
